package com.commerceE.domain;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Page implements java.io.Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7121631949604996094L;

	public static int DEFAULT_PAGE_SIZE=12;

	private int currentPageNum = 1;

	private int totlePageSize;

	private int itemSizePerPage = DEFAULT_PAGE_SIZE;

	private int totleItemSize;

	private String actionName;

	private List<Object> itemList;

	private boolean isPagination = false;
	
	private String requestType;

	public static String PAGE_ACTION_NAME_NEXT = "next";
	public static String PAGE_ACTION_NAME_PREVIOUS = "previous";
	public static String PAGE_ACTION_NAME_FIRST = "first";
	public static String PAGE_ACTION_NAME_LAST = "last";

	public Page calculate() {
		if (PAGE_ACTION_NAME_NEXT.equalsIgnoreCase(actionName)) {
			if (currentPageNum < totlePageSize) {
				currentPageNum = currentPageNum + 1;
			}
		} else if (PAGE_ACTION_NAME_PREVIOUS.equalsIgnoreCase(actionName)) {
			if (currentPageNum > 1) {
				currentPageNum = currentPageNum - 1;
			}
		} else if (PAGE_ACTION_NAME_FIRST.equalsIgnoreCase(actionName)) {
			currentPageNum = 1;
		} else if (PAGE_ACTION_NAME_LAST.equalsIgnoreCase(actionName)) {
			currentPageNum = totleItemSize;
		}
		return this;
	}
	
	public static int displayButtonNumber = 6;
	
	public static int displayButtonNumberBeforeCurrent = 3;
	
	public boolean firstPageButtonDisplayFlag = true;
	public boolean lastPageButtonDisplayFlag = true;
	public boolean previousPageButtonDisplayFlag = true;
	public boolean nextPageButtonDisplayFlag = true;
	
	public List<Long> getDisplayedPages(){
		List<Long> pageNumberList = new ArrayList<Long>();
		if(totlePageSize<=displayButtonNumber){
			for(long i=1L;i<=totlePageSize;i++){
				pageNumberList.add(i);
			}
		}else{
			if(currentPageNum<=displayButtonNumberBeforeCurrent){
				for(long i=1L;i<=6;i++){
					pageNumberList.add(i);
				}
			}else if((currentPageNum+3)>=totlePageSize){
				for(long i=(totlePageSize-5);i<=totlePageSize;i++){
					pageNumberList.add(i);
				}
			}else{
				for(long i=(currentPageNum-2);i<=(currentPageNum-2+5);i++){
					pageNumberList.add(i);
				}
			}
		}
		if(currentPageNum==1){
			firstPageButtonDisplayFlag=false;
			previousPageButtonDisplayFlag = false;
		}
		if(currentPageNum==totlePageSize){
			lastPageButtonDisplayFlag = false;
			nextPageButtonDisplayFlag  =false;
		}
		if(totlePageSize==1){
			firstPageButtonDisplayFlag=false;
			previousPageButtonDisplayFlag = false;
			lastPageButtonDisplayFlag = false;
			nextPageButtonDisplayFlag  =false;
		}
		return pageNumberList;
	}
	
	public static void main(String[] args){
		Page page = new Page();
		page.setCurrentPageNum(4);
		page.setTotlePageSize(7);
		List<Long> list = page.getDisplayedPages();
		for(Long id : list){
			System.out.println(id);
		}
	}

	public boolean isPagination() {
		return isPagination;
	}

	public String getRequestType() {
		return requestType;
	}

	public void setRequestType(String requestType) {
		this.requestType = requestType;
	}

	public void setPagination(boolean isPagination) {
		this.isPagination = isPagination;
	}

	public int getCurrentPageNum() {
		return currentPageNum;
	}

	public void setCurrentPageNum(int currentPageNum) {
		this.currentPageNum = currentPageNum;
	}

	public int getTotlePageSize() {
		return totlePageSize;
	}

	public void setTotlePageSize(int totlePageSize) {
		this.totlePageSize = totlePageSize;
	}

	public int getItemSizePerPage() {
		return itemSizePerPage;
	}

	public void setItemSizePerPage(int itemSizePerPage) {
		this.itemSizePerPage = itemSizePerPage;
	}

	public int getTotleItemSize() {
		return totleItemSize;
	}

	public void setTotleItemSize(int totleItemSize) {
		this.totleItemSize = totleItemSize;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public List<Object> getItemList() {
		return itemList;
	}

	public void setItemList(List<Object> itemList) {
		this.itemList = itemList;
	}

}
